package com.to8to.kitt.esb;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;

import com.to8to.kitt.ThriftLengthFieldBasedFrameDecoder;

public class BusServerChannelInitializer extends ChannelInitializer<Channel>{

	private TProtocolFactory protocalFactory = new TBinaryProtocol.Factory();
	
	private BusChannelHandler bizHandler;
	
	public BusServerChannelInitializer(TProtocolFactory protocalFactory, BusChannelHandler bizHandler)
	{
		this.protocalFactory = protocalFactory;
		this.bizHandler = bizHandler;
	}
	
	public BusServerChannelInitializer( )
	{
		
	}
	
	public TProtocolFactory getProtocalFactory() {
		return protocalFactory;
	}



	public void setProtocalFactory(TProtocolFactory protocalFactory) {
		this.protocalFactory = protocalFactory;
	}



	public BusChannelHandler getBizHandler() {
		return bizHandler;
	}



	public void setBizHandler(BusChannelHandler bizHandler) {
		this.bizHandler = bizHandler;
	}



	@Override
	protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        p.addLast(new LoggingHandler(LogLevel.TRACE));
        p.addLast(new ThriftLengthFieldBasedFrameDecoder());
        p.addLast(new LengthFieldPrepender(4));
        p.addLast(new ThriftBusMessageCodec(protocalFactory));
        p.addLast(bizHandler);
	}

}
